/*-
 * Copyright (c) 2009-2013 Shang-Rong Cai. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $ Bitmap: validate_bitmap_l.c    Rev. 1.2.2    2013-02-20T22:40+08:00 $
 */

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include <bscerr.h>
#include <bitmap.h>


#define TEST_LENGTH_MOD 1000000000


static const double TUT_NSEC = (double) 1000000000L;


int generate_random(bitmap_t *ba);

void print_full_bits(uint8_t sctr);

void print_part_bits(uint8_t sctr, size_t size);


int main(int argc, char *argv[])
{
    int ierr = ERRNO_UNDEFINED;

#ifdef DEBUG_INFO
    size_t zi, zj;  /* size_t based loop counter or index */
#endif

    double tt;  /* Total time */

    struct timespec tp_b;  /* Time of test begin */
    struct timespec tp_e;  /* Time of test end */

    size_t tssz;  /* Test sample size */

    bitmap_t btar;
    bitmap_t btsy;

    size_t nmkz;
    size_t nnnz;


#ifdef DEBUG_INFO
    printf("Check Function: print_full_bits()");

    for (zi = 0; zi < 256; zi++)
    {
        printf("%3d ", (uint8_t) zi);
        print_full_bits((uint8_t) zi);
        printf("\n");
    }

    printf("Check Function: print_part_bits()");

    for (zi = 7; zi > 0; zi--)
    {
        for (zj = 0; zj < 256; zj++)
        {
            printf("%3d ", (uint8_t) zj);
            print_part_bits((uint8_t) zj, zi);
            printf("\n");
        }
    }
#endif

    srandom((unsigned long) time(NULL));

    tssz = (size_t) random() % TEST_LENGTH_MOD + 30;
    printf("Test Sample Size: %zd\n", tssz);

    ierr = bitmap_init(&btar, tssz);
    ierr = bitmap_init(&btsy, tssz);

    ierr = generate_random(&btar);

    /* Test 1: */
#ifdef FREEBSD_EXTENSIONS
    ierr = clock_gettime(CLOCK_REALTIME_PRECISE, &tp_b);
#else
    ierr = clock_gettime(CLOCK_REALTIME, &tp_b);
#endif
    ierr = bitmap_get_marks(&btar, &nmkz);
#ifdef FREEBSD_EXTENSIONS
    ierr = clock_gettime(CLOCK_REALTIME_PRECISE, &tp_e);
#else
    ierr = clock_gettime(CLOCK_REALTIME, &tp_e);
#endif
    tt = difftime(tp_e.tv_sec, tp_b.tv_sec);
    tt += (double) (tp_e.tv_nsec - tp_b.tv_nsec) / TUT_NSEC;

    printf("Number of marks: %zd (By Old Method)        %f seconds\n", nmkz, tt);

    /* Test 2: */
#ifdef FREEBSD_EXTENSIONS
    ierr = clock_gettime(CLOCK_REALTIME_PRECISE, &tp_b);
#else
    ierr = clock_gettime(CLOCK_REALTIME, &tp_b);
#endif
    ierr = bitmap_count_marks(&btar, &nmkz);
#ifdef FREEBSD_EXTENSIONS
    ierr = clock_gettime(CLOCK_REALTIME_PRECISE, &tp_e);
#else
    ierr = clock_gettime(CLOCK_REALTIME, &tp_e);
#endif


    tt = difftime(tp_e.tv_sec, tp_b.tv_sec);
    tt += (double) (tp_e.tv_nsec - tp_b.tv_nsec) / TUT_NSEC;

    printf("Number of marks: %zd (Test! By New Method)  %f seconds\n", nmkz, tt);

    /* Test 3: */
#ifdef FREEBSD_EXTENSIONS
    ierr = clock_gettime(CLOCK_REALTIME_PRECISE, &tp_b);
#else
    ierr = clock_gettime(CLOCK_REALTIME, &tp_b);
#endif
    ierr = bitmap_get_nones(&btar, &nnnz);
#ifdef FREEBSD_EXTENSIONS
    ierr = clock_gettime(CLOCK_REALTIME_PRECISE, &tp_e);
#else
    ierr = clock_gettime(CLOCK_REALTIME, &tp_e);
#endif

    tt = difftime(tp_e.tv_sec, tp_b.tv_sec);
    tt += (double) (tp_e.tv_nsec - tp_b.tv_nsec) / TUT_NSEC;

    printf("Number of nones: %zd (By Old Method)        %f seconds\n", nnnz, tt);

    /* Test 4: */
#ifdef FREEBSD_EXTENSIONS
    ierr = clock_gettime(CLOCK_REALTIME_PRECISE, &tp_b);
#else
    ierr = clock_gettime(CLOCK_REALTIME, &tp_b);
#endif
    ierr = bitmap_count_nones(&btar, &nnnz);
#ifdef FREEBSD_EXTENSIONS
    ierr = clock_gettime(CLOCK_REALTIME_PRECISE, &tp_e);
#else
    ierr = clock_gettime(CLOCK_REALTIME, &tp_e);
#endif

    tt = difftime(tp_e.tv_sec, tp_b.tv_sec);
    tt += (double) (tp_e.tv_nsec - tp_b.tv_nsec) / TUT_NSEC;

    printf("Number of nones: %zd (Test! By New Method)  %f seconds\n", nnnz, tt);


    ierr = bitmap_dest(&btar);
    ierr = bitmap_dest(&btsy);

    return ierr;
}


int generate_random(bitmap_t *ba)
{
    int ierr = ERRNO_UNDEFINED;
    size_t zi;  /* size_t based loop counter or index */

#ifdef TRACE_INFO
    printf("[F] generate_random()\n");
#endif

    srandom((unsigned long) time(NULL));

    for (zi = 0; zi < ba->bksz; zi++)
    {
        ba->list[zi] = (uint8_t) (random() % 256);
    }

#ifdef TRACE_INFO
    printf("    generate_random() Passed!\n");
#endif

    return ierr;
}

void print_full_bits(uint8_t sctr)
{
    size_t zi;  /* size_t based loop counter or index */
    uint8_t quo;  /* Quotient */

    uint8_t dis[8];  /* Display form */

    quo = sctr;

    for (zi = 0; zi < 8; zi++)
    {
        if (quo % 2 == 0)
        {
            dis[7 - zi] = 0;
        }
        else
        {
            dis[7 - zi] = 1;
        }

        quo /= 2;
    }

    printf(" ");
    for (zi = 0; zi < 4; zi++)
    {
        printf("%d", dis[zi]);
    }

    printf(" ");
    for (zi = 4; zi < 8; zi++)
    {
        printf("%d", dis[zi]);
    }
}


void print_part_bits(uint8_t sctr, size_t size)
{
    size_t zi;  /* size_t based loop counter or index */
    uint8_t quo;  /* Quotient */

    uint8_t dis[8];  /* Display form */

    quo = sctr;

    for (zi = 0; zi < 8; zi++)
    {
        if (quo % 2 == 0)
        {
            dis[7 - zi] = 0;
        }
        else
        {
            dis[7 - zi] = 1;
        }

        quo /= 2;
    }

    printf(" ");
    for (zi = 0; zi < 4; zi++)
    {
        if (zi < size)
        {
            printf("%d", dis[zi]);
        }
        else
        {
            printf("-");
        }
    }

    printf(" ");
    for (zi = 4; zi < 8; zi++)
    {
        if (zi < size)
        {
            printf("%d", dis[zi]);
        }
        else
        {
            printf("-");
        }
    }
}

